package view.Tabs;

import java.util.Date;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;

import view.Items.SearchResultItemGUI;
import view.Windows.EditDetailsGUI;

import commons.Entities.LegenCharacterThin;
import commons.Entities.LegenDate;
import commons.Entities.User;

import controller.ControllerFunctions;

public class ProfileGUI {

	private final static ProfileGUI INSTANCE = new ProfileGUI();

	public static ProfileGUI get() {
		return INSTANCE;
	}

	public ProfileGUI() {

	}

	// final public Shell shlMain;
	protected Color cBackgroundColor;
	protected Font fHeadFont;
	protected Font fLabelFont;
	protected Font fButtonFont;
	protected Display mainDisp;
	protected Composite compMain;
	protected User user;
	protected ScrolledComposite scFavorites;
	protected Composite cFavorites;
	protected ScrollBar sbFavorites;
	protected ScrolledComposite scFutureDates;
	protected Composite cFutureDates;
	protected ScrollBar sbFutureDates;
	protected int numOfFutureDates = 0;
	protected ScrolledComposite scPastDates;
	protected Composite cPastDates;
	protected ScrollBar sbPastDates;
	protected int numOfPastDates = 0;
	protected CTabFolder ctfHome;
	protected List<LegenCharacterThin> lctList;
	protected List<LegenDate> ldList;

	protected Label lblUserName;
	protected Label lblFirstName;
	protected Label lblEmail;
	protected Label lblSex;
	protected Label lblAge;
	protected Label lblInterestedIn;

	public void ProfileGUIRun(CTabFolder ctfHome) {

		setCtfHome(ctfHome);
		initFonts(mainDisp);

		// main display for the login page
		compMain.setBounds(10, 10, 900, 700);
		setBackgroundColor(new Color(mainDisp, 240, 240, 240));
		Color clrWhite = new Color(mainDisp, 255, 255, 255);
		compMain.setForeground(clrWhite);

		// username
		lblUserName = new Label(compMain, 0);
		lblUserName.setBounds(60, 20, 300, 50);
		lblUserName.setFont(getHeadFont());
		lblUserName.setForeground(clrWhite);

		Composite grProfile = new Composite(compMain, 0);
		grProfile.setBounds(50, 80, 810, 660);
		grProfile.setForeground(clrWhite);

		// details group
		Group grUserDetails = new Group(grProfile, 0);
		grUserDetails.setText("User Details");
		grUserDetails.setBounds(5, 5, 360, 300);
		grUserDetails.setForeground(clrWhite);
		Label lblUserDetails = new Label(grProfile, 0);
		lblUserDetails.setForeground(clrWhite);
		lblUserDetails.setText("User Details");
		lblUserDetails.setBounds(15, 5, 85, 30);
		lblUserDetails.moveAbove(grUserDetails);
		lblUserDetails.setAlignment(SWT.CENTER);

		// name
		lblFirstName = new Label(grUserDetails, 0);
		lblFirstName.setBounds(30, 40, 250, 20);
		lblFirstName.setForeground(clrWhite);

		// Email
		lblEmail = new Label(grUserDetails, 0);
		lblEmail.setBounds(30, 70, 250, 20);
		lblEmail.setForeground(clrWhite);

		// sex
		lblSex = new Label(grUserDetails, 0);
		lblSex.setBounds(30, 100, 250, 20);
		lblSex.setForeground(clrWhite);

		// age
		lblAge = new Label(grUserDetails, 0);
		lblAge.setText("Date of Birth: " + user.getDateOfBirth());
		lblAge.setBounds(30, 130, 250, 20);
		lblAge.setForeground(clrWhite);

		// interested in
		lblInterestedIn = new Label(grUserDetails, 0);
		lblInterestedIn.setBounds(30, 160, 250, 20);
		lblInterestedIn.setForeground(clrWhite);

		// edit details button
		Button btnEditDetails = new Button(grUserDetails, SWT.NONE);
		btnEditDetails.setText("Edit Details");
		btnEditDetails.setBounds(120, 250, 120, 30);
		btnEditDetails.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				new EditDetailsGUI(mainDisp).showGUI(mainDisp);
			}
		});

		// future dates group
		Composite grFutureDates = new Composite(grProfile, SWT.INHERIT_DEFAULT);
		grFutureDates.setBounds(375, 5, 400, 150);
		grFutureDates.setForeground(clrWhite);
		grFutureDates.setBackgroundMode(SWT.INHERIT_FORCE);
		scFutureDates = new ScrolledComposite(grFutureDates, SWT.V_SCROLL | SWT.BORDER);
		scFutureDates.setBounds(0, 10, 400, 140);
		scFutureDates.setExpandVertical(true);
		cFutureDates = new Composite(scFutureDates, 0);
		sbFutureDates = scFutureDates.getVerticalBar();
		sbFutureDates.setVisible(true);
		Label lblFutureDates = new Label(grProfile, 0);
		lblFutureDates.setForeground(clrWhite);
		lblFutureDates.setText("Future Dates");
		lblFutureDates.setBounds(385, 5, 90, 30);
		lblFutureDates.moveAbove(grFutureDates);
		lblFutureDates.setAlignment(SWT.CENTER);

		// past dates group
		Composite grPastDates = new Composite(grProfile, 0);
		grPastDates.setBounds(375, 155, 400, 150);
		grPastDates.setForeground(clrWhite);
		grPastDates.setBackgroundMode(SWT.INHERIT_FORCE);
		scPastDates = new ScrolledComposite(grPastDates, SWT.V_SCROLL | SWT.BORDER);
		scPastDates.setBounds(0, 10, 400, 140);
		scPastDates.setExpandVertical(true);
		cPastDates = new Composite(scPastDates, 0);
		sbPastDates = scPastDates.getVerticalBar();
		sbPastDates.setVisible(true);
		Label lblPastDates = new Label(grProfile, 0);
		lblPastDates.setForeground(clrWhite);
		lblPastDates.setText("Past Dates");
		lblPastDates.setBounds(385, 155, 75, 30);
		lblPastDates.moveAbove(grPastDates);
		lblPastDates.setAlignment(SWT.CENTER);

		// favorites group
		Composite grFavorites = new Composite(grProfile, 0);
		grFavorites.setBounds(5, 310, 770, 220);
		grFavorites.setForeground(clrWhite);
		grFavorites.setBackgroundMode(SWT.INHERIT_FORCE);
		scFavorites = new ScrolledComposite(grFavorites, SWT.V_SCROLL | SWT.BORDER);
		scFavorites.setBounds(0, 10, 770, 210);
		scFavorites.setExpandVertical(true);
		cFavorites = new Composite(scFavorites, 0);
		sbFavorites = scFavorites.getVerticalBar();
		sbFavorites.setVisible(true);
		Label lblFavChar = new Label(grProfile, 0);
		lblFavChar.setForeground(clrWhite);
		lblFavChar.setText("Favorite Characters");
		lblFavChar.setBounds(15, 310, 135, 30);
		lblFavChar.moveAbove(grFavorites);
		lblFavChar.setAlignment(SWT.CENTER);

	}

	public Color getBackgroundColor() {
		return cBackgroundColor;
	}

	public void setBackgroundColor(Color cBackgroundColor) {
		this.cBackgroundColor = cBackgroundColor;
	}

	public Font getHeadFont() {
		return fHeadFont;
	}

	public void setHeadFont(Font fHeadFont) {
		this.fHeadFont = fHeadFont;
	}

	public Font getLabelFont() {
		return fLabelFont;
	}

	public void setLabelFont(Font fLabelFont) {
		this.fLabelFont = fLabelFont;
	}

	public Font getButtonFont() {
		return fButtonFont;
	}

	public void setButtonFont(Font fButtonFont) {
		this.fButtonFont = fButtonFont;
	}

	// initialize fonts
	protected void initFonts(Display display) {
		setHeadFont(new Font(display, new FontData("Fiolex Girls", 24, SWT.BOLD)));
		setLabelFont(new Font(display, new FontData("Ariel", 12, SWT.BOLD)));
		setButtonFont(new Font(display, new FontData("Ariel", 12, SWT.BOLD)));

	}

	public Display getMainDisp() {
		return mainDisp;
	}

	public void setMainDisp(Display mainDisp) {
		this.mainDisp = mainDisp;
	}

	public Composite getCompMain() {
		return compMain;
	}

	public void setCompMain(Composite compMain) {
		this.compMain = compMain;
	}

	public CTabFolder getCtfHome() {
		return ctfHome;
	}

	public void setCtfHome(CTabFolder ctfHome) {
		this.ctfHome = ctfHome;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<LegenCharacterThin> getLctList() {
		return lctList;
	}

	public void setLctList(List<LegenCharacterThin> lctList) {
		this.lctList = lctList;
	}

	public List<LegenDate> getLdList() {
		return ldList;
	}

	public void setLdList(List<LegenDate> ldList) {
		this.ldList = ldList;
	}

	/**
	 * update the favorites pane
	 * 
	 * @param lctList
	 */
	public void updateFavorites(final List<LegenCharacterThin> lctList) {
		final Color clrWhite = new Color(mainDisp, 255, 255, 255);
		setLctList(lctList);
		cFavorites.dispose();
		cFavorites = new Composite(scFavorites, 0);
		cFavorites.setBounds(0, 0, 740, 30 + lctList.size() * 50);
		sbFavorites.addListener(SWT.Selection, new Listener() {

			public void handleEvent(Event e) {
				Point location = cFavorites.getLocation();
				location.y = -sbFavorites.getSelection() * ((20 + lctList.size() * 50) / sbFavorites.getMaximum());
				cFavorites.setLocation(location);
			}
		});

		int i = 0;
		for (LegenCharacterThin lct : lctList) {
			new SearchResultItemGUI(lct.getCharacterID(), lct.getCharacterName(), lct.getGender(), lct.getSpecies(), lct.getAppearsInTheseFictionalUniverses(), 10, 15 + 50 * (i), cFavorites,
					mainDisp, ctfHome);
			i++;
		}

		if (i > 0) {
			Label lblHLine = new Label(cFavorites, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
			lblHLine.setBounds(10, 20 + lctList.size() * 50, 740, 10);
			lblHLine.setForeground(clrWhite);
		}

	}

	/**
	 * update the user future and past dates panes
	 * 
	 * @param ldList
	 */
	public void updateUserDates(final List<LegenDate> ldList) {
		final Color clrWhite = new Color(mainDisp, 255, 255, 255);
		setLdList(ldList);
		cFutureDates.dispose();
		cFutureDates = new Composite(scFutureDates, 0);
		cFutureDates.setBounds(0, 0, 740, 30 + ldList.size() * 50);
		cPastDates.dispose();
		cPastDates = new Composite(scPastDates, 0);
		cPastDates.setBounds(0, 0, 740, 30 + ldList.size() * 50);

		numOfFutureDates = 0;
		numOfPastDates = 0;
		Date today = new Date();
		today.getTime();
		for (final LegenDate ld : ldList) {

			if (ld.getDate().getTime() >= today.getTime()) {
				Button btnGoto = new Button(cFutureDates, SWT.NONE);
				btnGoto.setText("GoTo");
				btnGoto.setBounds(10, 15 + numOfFutureDates * 40, 40, 30);
				btnGoto.addSelectionListener(new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						if (HomePageGUI.get().characterPages.containsKey(ld.getCharacterID()))
							ctfHome.setSelection(HomePageGUI.get().characterPages.get(ld.getCharacterID()).getIndex());
						else
							ControllerFunctions.get().getCharacterDetails(ld.getCharacterID());
					}
				});

				Label lblName = new Label(cFutureDates, SWT.LEFT);
				lblName.setText(String.valueOf(ld.getCharacterName()));
				lblName.setBounds(60, 20 + numOfFutureDates * 40, 210, 20);
				lblName.setForeground(clrWhite);

				Label lblDate = new Label(cFutureDates, SWT.CENTER);
				lblDate.setText(String.valueOf(ld.getDate()));
				lblDate.setBounds(290, 20 + numOfFutureDates * 40, 80, 20);
				lblDate.setForeground(clrWhite);

				numOfFutureDates++;
			} else {
				Button btnGoto = new Button(cPastDates, SWT.NONE);
				btnGoto.setText("GoTo");
				btnGoto.setBounds(10, 15 + numOfPastDates * 40, 40, 30);
				btnGoto.addSelectionListener(new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						if (HomePageGUI.get().characterPages.containsKey(ld.getCharacterID()))
							ctfHome.setSelection(HomePageGUI.get().characterPages.get(ld.getCharacterID()).getIndex());
						else
							ControllerFunctions.get().getCharacterDetails(ld.getCharacterID());
					}
				});

				Label lblName = new Label(cPastDates, SWT.LEFT);
				lblName.setText(String.valueOf(ld.getCharacterName()));
				lblName.setBounds(60, 20 + numOfPastDates * 40, 210, 20);
				lblName.setForeground(clrWhite);

				Label lblDate = new Label(cPastDates, SWT.CENTER);
				lblDate.setText(String.valueOf(ld.getDate()));
				lblDate.setBounds(290, 20 + numOfPastDates * 40, 80, 20);
				lblDate.setForeground(clrWhite);

				numOfPastDates++;

			}
		}
		cFutureDates.setBounds(0, 0, 740, 30 + numOfFutureDates * 50);
		sbFutureDates.addListener(SWT.Selection, new Listener() {

			public void handleEvent(Event e) {
				Point location = cFutureDates.getLocation();
				location.y = -sbFutureDates.getSelection() * ((20 + numOfFutureDates * 40) / sbFutureDates.getMaximum());
				cFutureDates.setLocation(location);
			}
		});

		cPastDates.setBounds(0, 0, 740, 30 + numOfPastDates * 50);
		sbPastDates.addListener(SWT.Selection, new Listener() {

			public void handleEvent(Event e) {
				Point location = cPastDates.getLocation();
				location.y = -sbPastDates.getSelection() * ((20 + numOfPastDates * 40) / sbPastDates.getMaximum());
				cPastDates.setLocation(location);
			}
		});
	}

	/**
	 * update user details pane
	 */
	public void updateUserDetails() {
		setUser(HomePageGUI.get().getUser());
		lblUserName.setText(" " + getUser().getUserName());
		lblFirstName.setText("Name: " + getUser().getFirstName() + " " + user.getLastName());
		lblEmail.setText("Email: " + getUser().getEmail());
		lblSex.setText("Gender: " + getUser().getGender());
		lblInterestedIn.setText("Interested in: " + getUser().getIntrestedIn());
	}
}
